package com.sam.orbs;


import java.util.ArrayList;
import java.util.Random;
import java.util.Stack;
import java.util.Timer;
import java.util.TimerTask;

import android.R.color;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ViewFinder extends View implements OnTouchListener {
	private Orb ball1;
	private xTraLives heart;
	private String life = "LIFE: ";
	private long score = 0;
	private Paint p;
	private ArrayList<Enemy> enemy= new ArrayList<Enemy>();
	private ArrayList<xTraLives> lifeblock = new ArrayList<xTraLives>();
	private Context cxt;
	private ImageView bg;
	private TextView sc;
	private Timer timer = new Timer();
	private Random gen = new Random();
	TextView health;
	Stack<Enemy> enemyStack = new Stack<Enemy>();
	Stack<xTraLives> LifeStack = new Stack<xTraLives>();
	 Bitmap myBitmap; 
	 
	 
	 
	 
	public ViewFinder(Context context,TextView textView,TextView score) {
		super(context);
		cxt = context;
		BitmapFactory.Options opts = new BitmapFactory.Options();
		opts.inJustDecodeBounds=true;
		Bitmap temp = BitmapFactory.decodeResource(cxt.getApplicationContext().getResources(), R.drawable.space);
		myBitmap = Bitmap.createScaledBitmap(temp, 500, 800, false);
		setFocusable(true);
		health =  textView;
		sc = score;
		sc.setText(Long.toString(this.score));
		Toast.makeText(cxt, "You have 3 lives, dont get hit and you wont lose a life" , Toast.LENGTH_SHORT).show();
		ball1 = new Orb(50,50,Color.BLUE, context,R.drawable.sheep);
		health.setText(life + Integer.toString(ball1.gethitPoints()));
		//bg = (ImageView) findViewById(R.id.imageView1);
		enemy.add(new Enemy(100,100,5,Color.RED, context, R.drawable.enemy));
		lifeblock.add(new xTraLives(100,100,5,Color.MAGENTA,context));
		this.setOnTouchListener(this);

		TimerTask Etask = new TimerTask(){

			@Override
			public void run() {
				notifyMe();

			}

		};
		timer.schedule(Etask,0, 1000);
 
		TimerTask Ltask = new TimerTask(){

			@Override
			public void run() {
				LifeCube();

			}

		};
		timer.schedule(Ltask,0, 10000);
		
		TimerTask Ptask = new TimerTask(){

			@Override
			public void run() {
				incScore();
				

			}

		};
		timer.schedule(Ptask, 0,10);
	}
	@Override
	public void onDraw(Canvas c){
		
		sc.setText(Long.toString(score));
		p = new Paint();
		 c.drawBitmap(myBitmap, 0, 0, p);
		p.setColor(Color.RED);
		p.setAntiAlias(true);

		for(int i = 0; i<enemy.size(); i++){
			enemy.get(i).eMove();
			c.drawBitmap(enemy.get(i).getImg(), enemy.get(i).getX()- enemy.get(i).getRadius(),enemy.get(i).getY()-enemy.get(i).getRadius(), p);
		}
		p.setColor(Color.BLUE);
		
		c.drawBitmap(ball1.getImg(), ball1.getX()- ball1.getRadius(),ball1.getY()-ball1.getRadius(), p);
		
		p.setColor(Color.MAGENTA);
		for(int i = 0; i<lifeblock.size();i++){
			lifeblock.get(i).eMove();
			c.drawCircle(lifeblock.get(i).getX(), lifeblock.get(i).getY(), 25, p);
		}
       
		invalidate();
		
		
	}
	public boolean onTouch(View v, MotionEvent event) {

		
		for(int i = 0; i<enemy.size(); i++){

			if(event.getX() >= (enemy).get(i).getX() -25 && event.getX() <= enemy.get(i).getX() + 25 && event.getY() >= enemy.get(i).getY() - 25 && event.getY() <= enemy.get(i).getY() + 25){
				ball1.setX((int) event.getX());
				ball1.setY((int) event.getY());
				ball1.sethitPoints(ball1.gethitPoints() - 1);
			
				
				health.setText(life + ball1.gethitPoints());
				if(ball1.gethitPoints() <= 0){
					ball1.isMovable(false);
					Toast.makeText(cxt, "GAME OVER", Toast.LENGTH_LONG);
					enemyStack.push(enemy.get(i));
				}
			}
}
		
			for(int a = 0; a <lifeblock.size(); a ++){
				if(event.getX() >= (lifeblock).get(a).getX() -25 && event.getX() <= lifeblock.get(a).getX() + 25 && event.getY() >= lifeblock.get(a).getY() - 25 && event.getY() <= lifeblock.get(a).getY() + 25){
					ball1.sethitPoints(ball1.gethitPoints() + 1);
					health.setText(life + ball1.gethitPoints());
					Toast.makeText(cxt, "+1 LIFE" , Toast.LENGTH_SHORT).show();
					
					try{
					LifeStack.push(lifeblock.get(a));
					}
					catch(Exception e){
						
					}
				}
							

			}

		

		if( ball1.gethitPoints() > 0)
		{
			ball1.setX((int) event.getX());
			ball1.setY((int) event.getY());
		}
		return true;
	}
	private void notifyMe(){

		enemy.add(new Enemy(gen.nextInt(480),-10,gen.nextInt(10)-5,Color.RED,cxt,R.drawable.enemy));
		Log.d("add", "enemy");	
		for(int i = 0; i<enemy.size(); i++){
			if(enemy.get(i).getY() > 800){
				enemyStack.push(enemy.get(i));
				
			}}
		
		for(int a = 0; a <lifeblock.size(); a ++){
			if(lifeblock.get(a).getY() > 800){
			LifeStack.push(lifeblock.get(a));
			}
		}
		while( !enemyStack.empty()) {
			enemy.remove(enemyStack.pop());
			Log.d("delete", "delete enemy");
		}
		while( !LifeStack.empty()) {
			lifeblock.remove(LifeStack.pop());
			Log.d("delete", "delete life");
		}
	}
	private void LifeCube(){
		lifeblock.add(new xTraLives(gen.nextInt(480),-10,gen.nextInt(10)-5, Color.MAGENTA,cxt));
		Log.d("add", "life");
		

	}
	private void incScore(){
		score++;
		
	}

}
